﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace UpgradeLoader
{
    [XmlRoot("SoftwareUpdate")]
    public class SoftwareUpdate
    {
        [XmlElement("VersionInfo")]
        public VersionInfo VersionInfo { get; set; }

        [XmlElement("UpdateServer")]
        public UpdateServer UpdateServer { get; set; }

        [XmlElement("UpdatePackages")]
        public UpdatePackages UpdatePackages { get; set; }

        /// <summary>
        /// 1:弹框提示用户升级  2：强制升级
        /// </summary>
        [XmlElement("UpdatePolicy")]
        public int UpdatePolicy { get; set; } // 直接解析为整数

        [XmlElement("Logging")]
        public Logging Logging { get; set; }

        [XmlElement("ApplicationPath")]
        public string ApplicationPath { get; set; }
    }

    // 版本信息
    public class VersionInfo
    {
        [XmlElement("CurrentVersion")]
        public string CurrentVersion { get; set; }

        [XmlElement("LatestVersion")]
        public string LatestVersion { get; set; }

        public int CurrentVersionNum
        {
            get
            {
                if (string.IsNullOrWhiteSpace(CurrentVersion))
                {
                    return 0;
                }
                var temp = CurrentVersion.Replace(".", "");
                return Convert.ToInt32(temp);
            }
        }
    }

    // 更新服务器配置
    public class UpdateServer
    {
        [XmlElement("PrimaryServerURL")]
        public string PrimaryServerURL { get; set; }

        [XmlElement("FallbackServerURL")]
        public string FallbackServerURL { get; set; }
    }

    // 升级包信息
    public class UpdatePackages
    {
        [XmlElement("UpdateItem")]
        public List<UpdateItem> UpdateItems { get; set; }
    }

    // 单个升级包
    public class UpdateItem
    {
        [XmlAttribute("FileName")]
        public string FileName { get; set; }

        [XmlAttribute("Zip")]
        public bool Zip { get; set; }

        [XmlAttribute("size")]
        public long Size { get; set; }

        [XmlAttribute("MD5")]
        public string MD5 { get; set; }

        public bool IsFinish { get; set; }
    }

    public class DownloadItem
    {
        public string DownUrl { get; set; }
        public string TempFileName { get; set; }
        public long DownOffset { get; set; }
        public bool IsFinish { get; set; }
        public string DestFileName { get; set; }

        public string LocalDir { get; set; }
        public bool IsDownSuccess { get; set; }

        public bool IsZip { get; set; }
    }


    // 日志与错误处理
    public class Logging
    {
        [XmlElement("LogPath")]
        public string LogPath { get; set; }

        [XmlElement("ErrorHandling")]
        public string ErrorHandling { get; set; }
    }

    public class DowningFile
    {
        public int Version { get; set; }
        public List<DownloadItem> Items { get; set; }
        public bool isCanUpgrade { get; set; }
    }
}
